﻿#include "stdafx.h"
#include "MainWindow.h"
#include "FrameInclude.h"
#include "SheetInclude.h"

static int iTimeCount = 0;

MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	setWindowIcon(QIcon("image:/sys/appIcon.png"));
	setAttribute(Qt::WA_DeleteOnClose);

	initMainWindow();
}

MainWindow::~MainWindow()
{
	if (pTimer)
	{
		pTimer->stop();
		pTimer->deleteLater();
		pTimer = NULL;
	}

	CHK_POINT_DESTORY(pSheetList);
	CHK_POINT_DESTORY(pCzpListModel);
	CHK_POINT_DESTORY(pFrameHelper);
	CHK_POINT_DESTORY(pSheetInterface);
	CHK_POINT_DESTORY(pQuit);
	CHK_POINT_DESTORY(pChangePassword);
	CHK_POINT_DESTORY(pLogOff);
	CHK_POINT_DESTORY(pMenu);
}
/***********************************************************************************************************************************/
/***********************************************************************************************************************************/
void MainWindow::initMainWindow()
{
	pFrameHelper = new FrameHelper(this);
	pFrameHelper->addEdgeWidget(this);
	pFrameHelper->addDragWidget(this);
	pFrameHelper->setCloseButton(ui.titleBar->getCloseBtn());
	pFrameHelper->setMinimizedButton(ui.titleBar->getMinBtn());
	pFrameHelper->setMaxRestoreButton(ui.titleBar->getMaxBtn());
	/*Time Index User*/
	displayTime();
	pTimer = new QTimer;
	connect(pTimer, SIGNAL(timeout()), this, SLOT(onTimer()));
	pTimer->start(500);

	hideWidgets();
	initCzpList();
	initMenu();
	initUIData();

	pSheetList = NULL;
	pSheetInterface = NULL;

	connect(ui.tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(on_tabWidget_close(int)));
	connect(ui.pageIndex, SIGNAL(sheetListDoubleClicked(int, int, const QString &)), this, SLOT(on_sheetListView_doubleClicked(int, int, const QString &)));
}

void MainWindow::initCzpList()
{
	pCzpListModel = new QRecordTreeModel;
	ui.listCzpList->setModel(pCzpListModel);
	QRecordTree &recordTree = pCzpListModel->records();
	recordTree.addCol("ID", CT_INT, CF_PRIMARY, "ID");
	recordTree.addCol("PID", CT_INT, CF_NONE, "PID");
	recordTree.addCol("NODENAME", CT_STRING, CF_NONE, "NODENAME");
	recordTree.addCol("SQLFILTER", CT_STRING, CF_NONE, "SQLFILTER");

	QVariantList list1;  list1.append(1);   list1.append(0); list1.append(tr("daihuitian")); list1.append("SHEETTYPE = 1 AND SHEETTYPE <> 2 AND (SHEETSTATUSID=2 OR SHEETSTATUSID=3)");
	QVariantList list2;  list2.append(11);  list2.append(1); list2.append(tr("yizhi"));   list2.append("deptid='1D9099E2-31C9-42BF-97FD-B82773ED8587'");
	QVariantList list3;  list3.append(12);  list3.append(1); list3.append(tr("erzhi"));   list3.append("deptid='7D8CE771-C9D9-4342-8CA5-B6E82D0724FF'");
	QVariantList list4;  list4.append(13);  list4.append(1); list4.append(tr("sanzhi"));   list4.append("deptid='B4945E0B-A92D-4B0E-8486-C6718A1F6BEC'");
	QVariantList list5;  list5.append(14);  list5.append(1); list5.append(tr("sizhi"));   list5.append("deptid='6CA40154-98A3-40A3-8AB1-CC7D8F2DBC15'");
	QVariantList list6;  list6.append(15);  list6.append(1); list6.append(tr("wuzhi"));   list6.append("deptid='D155CF6A-EA5C-442E-B262-0FE3878DD022'");
	QVariantList list7;  list7.append(2);   list7.append(0); list7.append(tr("cundang"));   list7.append("SHEETTYPE = 1 AND SHEETTYPE <> 2 AND (SHEETSTATUSID=6)");
	QVariantList list8;  list8.append(21);  list8.append(2); list8.append(tr("yizhi"));   list8.append("deptid='1D9099E2-31C9-42BF-97FD-B82773ED8587'");
	QVariantList list9;  list9.append(22);  list9.append(2); list9.append(tr("erzhi"));   list9.append("deptid='7D8CE771-C9D9-4342-8CA5-B6E82D0724FF'");
	QVariantList list10; list10.append(23); list10.append(2); list10.append(tr("sanzhi"));   list10.append("deptid='B4945E0B-A92D-4B0E-8486-C6718A1F6BEC'");
	QVariantList list11; list11.append(24); list11.append(2); list11.append(tr("sizhi"));   list11.append("deptid='6CA40154-98A3-40A3-8AB1-CC7D8F2DBC15'");
	QVariantList list12; list12.append(25); list12.append(2); list12.append(tr("wuzhi"));   list12.append("deptid='D155CF6A-EA5C-442E-B262-0FE3878DD022'");
	recordTree.addRow(list1);
	recordTree.addRow(list2);
	recordTree.addRow(list3);
	recordTree.addRow(list4);
	recordTree.addRow(list5);
	recordTree.addRow(list6);
	recordTree.addRow(list7);
	recordTree.addRow(list8);
	recordTree.addRow(list9);
	recordTree.addRow(list10);
	recordTree.addRow(list11);
	recordTree.addRow(list12);
	pCzpListModel->setCol(0, CF_PRIMARY);
	pCzpListModel->setCol(2, CF_SHOW);
	pCzpListModel->setFieldCol(0, 1);
	pCzpListModel->setRoot(0);
	pCzpListModel->refresh();
	ui.listCzpList->expandAll();
}

void MainWindow::hideWidgets()
{
	ui.listCzpList->setVisible(false);
}

void MainWindow::displayTime()
{
	iTimeCount++;
	if (iTimeCount >= 60)
	{
		iTimeCount = 0;
	}
	const QDateTime & time = getServerTime();
	const QString & strTime = time.toString("hh:mm");
	const QString & strNextTime = time.toString("hh mm");

	if (iTimeCount % 2 == 0)
	{
		ui.lcdTime->display(strTime);
	} 
	else
	{
		ui.lcdTime->display(strNextTime);
	}
}

void MainWindow::setWidgetStatus(QToolButton * btn, QWidget * w)
{
	if (btn->isChecked())
	{
		w->setVisible(true);
		btn->setArrowType(Qt::DownArrow);
	}
	else
	{
		w->setVisible(false);
		btn->setArrowType(Qt::RightArrow);
	}
}

bool MainWindow::deleteTabWidget(QWidget * widget)
{
    CHK_EXP_RET(widget == NULL, false);

	if (widget == pSheetList)
	{
		delete pSheetList;
		pSheetList = NULL;
	}
	else
	{
        return pSheetInterface->closeSheet(widget);
	}
    return true;
}

void MainWindow::setTabWidget(QWidget * widget, const QString & name)
{
	int pos = ui.tabWidget->indexOf(widget);
	if (pos == -1)
	{
		pos = ui.tabWidget->addTab(widget, name);
		ui.tabWidget->setTabToolTip(pos, name);
	}

	ui.tabWidget->setCurrentIndex(pos);
	ui.stackedWidget->setCurrentIndex(1);
}

/***********************************************************************************************************************************/
void MainWindow::on_btnCzpList_clicked()
{
	setWidgetStatus(ui.btnCzpList, ui.listCzpList);

}

void MainWindow::on_btnIndex_clicked()
{
	//ui.pageIndex->updateHomePageList();
	ui.stackedWidget->setCurrentIndex(0);
}

void MainWindow::on_listCzpList_clicked(const QModelIndex & index)
{
	//操作票列表
	if (pSheetList == NULL)
	{
		pSheetList = new SheetListCtrl;
		connect(pSheetList, SIGNAL(doubleClicked(int, int, const QString &)), this, SLOT(on_sheetListView_doubleClicked(int, int, const QString &)));
	}
	
	//清空查询条件
	pSheetList->initQueryCondition();

	//设置Tab页
	setTabWidget(pSheetList, tr("czpList"));

	//填充数据
	QModelIndex curIndex = index;
	int iSheetBox = 0;
	if (pCzpListModel->records().getCollectInt(curIndex.internalId(), "ID") == 1 || pCzpListModel->records().getCollectInt(curIndex.internalId(), "PID") == 1)
	{
		iSheetBox = 1;
	}
	else
	{
		iSheetBox = 2;
	}
	QString sqlfilter = pCzpListModel->records().getCollectStr(curIndex.internalId(), "SQLFILTER");
	//int listid = pCzpListModel->records().data(curIndex.internalId(), 4).toInt();
	curIndex = pCzpListModel->parent(curIndex);
	while (curIndex != QModelIndex())
	{
		sqlfilter = sqlfilter + " AND " + pCzpListModel->records().getCollectStr(curIndex.internalId(), "SQLFILTER");
		curIndex = pCzpListModel->parent(curIndex);
	}
	sqlfilter += "	AND PATTERNID IN (SELECT PATTERNID FROM WS_PATTERNINFO WHERE MODULETYPE = 2) ";
	pSheetList->initialise(sqlfilter, 5, iSheetBox, true);
}

void MainWindow::openTicket(int sheetid, int sheetBox, const QString & sheetname)
{
	ui.toolBox->setVisible(false);
	ui.btnHide->setChecked(true);
	if (pSheetInterface == NULL)
		pSheetInterface = new SheetInterface();

	if (pSheetInterface->curOpenSheetCount()!=0)
	{
		qMsgBoxAlert(tr("please close current opensheet!"));
		return;
	}

	QWidget * sheetView = pSheetInterface->openSheet(sheetid,sheetBox);

	if (sheetView)
	{
		setTabWidget(sheetView, sheetname);
		connect(sheetView, SIGNAL(closeSheetSignal(int)), this, SLOT(on_tabWidget_close(int)));
	}
}

void MainWindow::on_sheetListView_doubleClicked(int sheetid, int sheetBox, const QString & strName)
{
	openTicket(sheetid, sheetBox, strName);
}

void MainWindow::on_tabWidget_close(int index)
{
	//关闭窗口， 若index<0 则关闭当前窗口。
	CHK_EXP_RUN(index < 0, index = ui.tabWidget->currentIndex());
	QWidget * curWidget = ui.tabWidget->widget(index);
	if (curWidget)
	{
        if(deleteTabWidget(curWidget))
            ui.tabWidget->removeTab(index);
	}
	if (ui.tabWidget->count() <= 0)
	{
		ui.stackedWidget->setCurrentIndex(0);
	}
}

void MainWindow::onTimer()
{
	displayTime();
}

void MainWindow::on_btnOption_clicked()
{
	//QMainWindow左上角坐标
	int iX = this->pos().x();
	int iY = this->pos().y();

	//btnOption按钮的左上角坐标
	QPoint p = ui.btnOption->pos();
	int iHeight = ui.btnOption->rect().height();
	int iWidth = ui.btnOption->rect().width();

	pMenu->popup(QPoint(p.x() + iX + iWidth, p.y() + iY + iHeight));
}

void MainWindow::on_btnHide_clicked()
{
	if (ui.btnHide->isChecked())
	{
		ui.toolBox->setVisible(false);
	}
	else
	{
		ui.toolBox->setVisible(true);
	}
}

void MainWindow::initMenu()
{
	pLogOff = new QAction(tr("LogOff"),this);
	connect(pLogOff, SIGNAL(triggered(bool)), this, SLOT(on_pLogOff(bool)));

	pQuit = new QAction(tr("Quit"), this);
	connect(pQuit, SIGNAL(triggered(bool)), this, SLOT(on_pQuit(bool)));

	pChangePassword = new QAction(tr("ChangePwd"), this);
	connect(pChangePassword, SIGNAL(triggered(bool)), this, SLOT(on_pChangePassword(bool)));

	pMenu = new QMenu(this);
	pMenu->addAction(pChangePassword);
	pMenu->addAction(pLogOff);
	pMenu->addAction(pQuit);
}

void MainWindow::on_pLogOff(bool checked)
{
	this->close();
	QCoreApplication::exit(2);
}

void MainWindow::on_pQuit(bool checked)
{
	this->close();
}

void MainWindow::on_pChangePassword(bool checked)
{
	LoginUserInfo * userInfo = LoginUserInfo::getInstance();
	Dialog dlg;
	ChangeUserPwd pChangePwdDlg(userInfo->getUserName(), userInfo->getUserId(),userInfo->getPasswd());
	connect(&pChangePwdDlg, SIGNAL(changePasswd(bool)), this, SLOT(changePasswd(bool)));
	dlg.setWidget(&pChangePwdDlg);
	dlg.setMinBtnVisible(false);
	dlg.setMaxBtnVisible(false);
	dlg.setFixedSize(306, 180);
	dlg.setTitle(tr("Change Password"));
	dlg.exec();
}

void MainWindow::initUIData()
{
	connect(ui.btnOption, SIGNAL(showMenu()), this, SLOT(on_btnOption_clicked()));
	ui.labCurUserName->setText(LoginDlg::getUserInfo().strUserName);
	ui.labCurUserDept->setText(LoginDlg::getUserInfo().strDeptName);
}

void MainWindow::changePasswd(bool bSuceess)
{
	if (bSuceess)
	{
		qMsgBoxInfo(tr("Change password success!"));
	} 
	else
	{
		qMsgBoxAlert(tr("Change password failed!"));
	}
}


void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
	QPoint p = mapToGlobal(event->pos());
	if (event->button() == Qt::LeftButton && ui.titleBar->getDoubleClickResponseRect().contains(p))
	{	
		pFrameHelper->convertMaxAndRestore();
	}
}


